package com.spider.gp.processor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spider.gp.dao.GpHyDao;
import com.spider.gp.dao.HangYe;
import com.spider.gp.dao.Hy;
import com.spider.gp.dao.HyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.HashMap;
import java.util.List;

/**
 * description:
 *
 * @author clare
 * @date 2022/05/20 08:56
 **/
@Component
public class HyProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Autowired
    private HyDao hyDao;

    @Override
    public void process(Page page) {

        LambdaQueryWrapper<Hy> wrapper = new LambdaQueryWrapper<>();
        hyDao.delete(wrapper);

        List<String> all = page.getJson().jsonPath("$.data.diff").all();
        all.parallelStream().forEach(item ->   {
            HashMap hashMap = JSON.parseObject(item, HashMap.class);
            String code  = hashMap.get("f12").toString();
            String name = hashMap.get("f14").toString();
            hyDao.insert(Hy.builder().name(name).code(code).build());
        });
    }

    @Override
    public Site getSite() {
        return site;
    }
}








